<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Requests\Api\ApiRequest as Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Interfaces;
use App\Interfaces\CartService;

use App\Exceptions\Interfaces\CartServiceException;
use App\Exceptions\Api\ApiDoneErrorException;
use App\Exceptions\Api\ApiInputErrorException;
use App\Exceptions\Api\ApiRsaEecryptErrorException;
use App\Exceptions\Api\ApiUserNotLoginErrorException;
use App\Exceptions\Api\ApiValidateErrorException;
use DB;

class CartController extends Controller
{

    /**
     * 会话类型  SESSION|USER
     * @var
     */
    protected $type;

    /**
     * 购物车回话ID
     * @var
     */
    protected  $reference_id;

    public function __construct(Request $request, CartService $cartService)
    {
        if (isset($request->apiUser->get()->id)) {
            $type = 'USER';
            $reference_id = $request->apiUser->get()->id;
        }elseif (isset($request->apiSession->session_id)) {
            $type = 'SESSION';
            $reference_id = $request->apiSession->session_id;
        }

        $this->init($type, $reference_id);
        $cartService->init($type,$reference_id);

    }

    public function init($type,$reference_id){

        if (!in_array($type,['SESSION','USER']))
        {
            throw new ApiUserNotLoginErrorException("type参数类型错误,必须为SESSION或USER");
        }

        $this->type = $type;

        $this->reference_id = $reference_id;

    }

    private function getTypeFiled(){

        if ($this->type=='SESSION') $filed = 'session_id';
        if ($this->type=='USER') $filed = 'user_id';
        return $filed;
    }

    /**
     * @api {post} /cart/get_cart_goods 获取购物车商品.
     * @apiName Cart/getCartGoods
     * @apiGroup Cart
     *
     * @apiSuccess {Object[]}  Object      购物车商品信息
     * @apiSuccess {Number}  Object.cart_id         购物车id
     * @apiSuccess {Number}  Object.user_id         用户id
     * @apiSuccess {Number}  Object.session_id      session id
     * @apiSuccess {Number}  Object.goods_id        商品id
     * @apiSuccess {Number}  Object.buy_num         购买数量
     * @apiSuccess {String}  Object.goods_attr      商品属性描述
     * @apiSuccess {String}  Object.goods_attr_id   商品属性值id集合
     * @apiSuccess {Number}  Object.cat_id          分类id
     * @apiSuccess {String}  Object.goods_sn        商品编号
     * @apiSuccess {String}  Object.goods_name      商品名称
     * @apiSuccess {String}  Object.goods_pet_name         购物车商品副标题
     * @apiSuccess {String}  Object.goods_desc      商品简介
     * @apiSuccess {Number}  Object.brand_id        品牌id
     * @apiSuccess {Number}  Object.goods_number    商品库存数量
     * @apiSuccess {Number}  Object.goods_weight    商品重量
     * @apiSuccess {Number}  Object.market_price    商品市场价格
     * @apiSuccess {Number}  Object.shop_price      商品商城价格
     * @apiSuccess {String}  Object.keywords        商品关键词
     * @apiSuccess {String}  Object.goods_content   商品内容
     * @apiSuccess {String}  Object.goods_thumb     商品缩略图
     * @apiSuccess {Number}  Object.type            商品类型，0：一般商品，1：e贸易商品
     * @apiSuccess {Number}  Object.goods_gallery_id         商品相册
     * @apiSuccess {Number}  Object.is_on_sale              商品是否上架，1：上架；2：下架
     * @apiSuccess {Number}  Object.add_time                商品添加时间
     * @apiSuccess {Number}  Object.sort_order              商品排序
     * @apiSuccess {Number}  Object.is_delete               商品是否已被删除（目前不用）
     * @apiSuccess {Number}  Object.is_home_recommend       商品是否首页推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.is_cat_recommend        商品是否分类推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.shop_id                 商品所属商铺id
     * @apiSuccess {Number}  Object.sales_num               商品销售数量
     * @apiSuccess {String}  Object.deleted_at                商品删除时间（默认为空，如果不为空则该商品已被删除）
     * @apiSuccess {Number}  Object.search_keywords         商品搜索关键词
     * @apiSuccess {Number}  Object.warehouse_id            商品所在仓库id（暂不可用）
     * @apiSuccess {Number}  Object.created_at              商品创建时间
     * @apiSuccess {Number}  Object.updated_at              商品编辑/更新时间
     * @apiSuccess {Number}  Object.audit_status            商品核审状态：0：未审核；1：审核通过；2：审核不通过
     * @apiSuccess {Number}  Object.goods_bar_code          商品条形码
     * @apiSuccess {String}  Object.mobile_goods_thumb      商品图片完整地址
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //获取购物车商品成功
     * [
     *     {
     *         "cart_id": 112,
     *         "user_id": 0,
     *         "session_id": "d0dca34cce180ad3f86f1230f4c28a343a3cadcf",
     *         "goods_id": 1346,
     *         "buy_num": 12,
     *         "goods_attr": "",
     *         "goods_attr_id": "",
     *         "cat_id": 400,
     *         "goods_sn": "010101",
     *         "goods_name": "莲花香",
     *         "goods_pet_name": "莲花香牌男士香水",
     *         "goods_desc": "莲花香牌男士香水",
     *         "brand_id": 79,
     *         "goods_number": 100,
     *         "goods_weight": "0.000",
     *         "market_price": "199.00",
     *         "shop_price": "99.00",
     *         "keywords": "",
     *         "goods_content": "",
     *         "goods_thumb": "",
     *         "resource": "中国",
     *         "type": 0,
     *         "goods_gallery_id": 0,
     *         "is_on_sale": 1,
     *         "add_time": 0,
     *         "sort_order": 100,
     *         "is_delete": 0,
     *         "is_home_recommend": 0,
     *         "is_cat_recommend": 0,
     *         "shop_id": 5,
     *         "sales_num": 0,
     *         "deleted_at": null,
     *         "search_keywords": ",4ASCENT,美妆个护,美容彩妆,香水/香氛,香水,香氛,中性香水,香水,中性",
     *         "warehouse_id": 0,
     *         "created_at": "2016-04-01 14:13:29",
     *         "updated_at": "2016-04-01 14:49:08",
     *         "audit_status": 1,
     *         "goods_bar_code": "010101",
     *         "mobile_goods_thumb": ""
     *     },
     *     {
     *         "cart_id": 113,
     *         "user_id": 0,
     *         "session_id": "d0dca34cce180ad3f86f1230f4c28a343a3cadcf",
     *         "goods_id": 1343,
     *         "buy_num": 2,
     *         "goods_attr": "",
     *         "goods_attr_id": "",
     *         "cat_id": 398,
     *         "goods_sn": "33",
     *         "goods_name": "33",
     *         "goods_pet_name": "33",
     *         "goods_desc": "33",
     *         "brand_id": 74,
     *         "goods_number": 33,
     *         "goods_weight": "0.000",
     *         "market_price": "33.00",
     *         "shop_price": "33.00",
     *         "keywords": "33",
     *         "goods_content": "",
     *         "goods_thumb": "8a75e0739bad792a326d8153dc6306fe",
     *         "resource": "33",
     *         "type": 1,
     *         "goods_gallery_id": 0,
     *         "is_on_sale": 1,
     *         "add_time": 0,
     *         "sort_order": 100,
     *         "is_delete": 0,
     *         "is_home_recommend": 0,
     *         "is_cat_recommend": 0,
     *         "shop_id": 1,
     *         "sales_num": 0,
     *         "deleted_at": null,
     *         "search_keywords": "33,JJBIO,美妆个护,美容彩妆,香水/香氛,香水,香氛,女士香水,香水,女士",
     *         "warehouse_id": 0,
     *         "created_at": "2016-02-19 10:10:49",
     *         "updated_at": "2016-03-16 12:03:42",
     *         "audit_status": 1,
     *         "goods_bar_code": null,
     *         "mobile_goods_thumb": "http://afutest.img-cn-qingdao.aliyuncs.com/8a75e0739bad792a326d8153dc6306fe.jpg"
     *     }
     * ]
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 406
     * //获取购物车商品失败
     * {
     *   "message": "当前用户类型不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 406
     * //获取购物车商品失败
     * {
     *   "message": "当前用户类型值不能为空"
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 500
     * //获取购物车商品失败
     * {
     *   "message": "type参数类型错误,必须为SESSION或USER"
     * }
     */
    public function getCartGoods(Request $request, CartService $cartService)
    {
        $type = $this->getTypeFiled();
        $reference_id = $this->reference_id;

        if (empty($type)) {

            throw new ApiInputErrorException("当前用户类型不能为空");
        }//end if()

        if (empty($reference_id)) {

            throw new ApiInputErrorException("当前用户类型值不能为空");
        }//end if()

        try {

            $return = $cartService->getCartGoods();
            return response()->json($return);

        }catch(CartServiceException $e){

            throw new ApiDoneErrorException($e->getMessage());
        }//end try

    }//end func getCartGoods()


    /**
     * @api {post} /cart/add_goods 将商品加入购物车.
     * @apiName Cart/addGoods
     * @apiGroup Cart
     * @apiDescription 将商品加入购物车.
     * @apiParam {Number} goods_id  商品id.
     * @apiParam {Number} goods_number  商品数量.
     *
     * @apiSuccess {string}  message    处理的结果
     * @apiSuccessExample {json} Success-Response01:
     * HTTP/1.1 200 OK
     * //添加商品到购物车成功
     * {
     *   "result": "true"
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //添加商品到购物车失败
     * {
     *     "message": "商品id不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //添加商品到购物车失败
     * {
     *     "message": "商品数量不能为空"
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 202
     * //添加商品到购物车失败
     * {
     *     "message": "该商品已下架"
     * }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 202
     * //添加商品到购物车失败
     * {
     *     "message": "该商品库存不足"
     * }
     * @apiErrorExample Error-Response 05:
     * HTTP/1.1 202
     * //添加商品到购物车失败
     * {
     *     "message": "该商品不存在或已删除"
     * }
     * @apiErrorExample Error-Response 06:
     * HTTP/1.1 202
     * //添加商品到购物车失败
     * {
     *   "message": "type参数类型错误,必须为SESSION或USER"
     * }
     */
    public function addGoods(Request $request, CartService $cartService)
    {
        $type = $this->getTypeFiled();
        $reference_id = $this->reference_id;

        if (empty($type)) {

            throw new ApiInputErrorException("当前用户类型不能为空");
        }//end if()

        if (empty($reference_id)) {

            throw new ApiInputErrorException("当前用户类型值不能为空");
        }//end if()

        $goods_id = $request->get('goods_id');
        $goods_number = $request->get('goods_number');

        if(empty($request->get('goods_id'))){

            throw new ApiInputErrorException("商品id不能为空");

        }//end if()

        if(empty($request->get('goods_number'))){

            throw new ApiInputErrorException("商品数量不能为空");

        }//end if()

        try{

            $cart = $cartService->addGoods($goods_id,$goods_number);

            if ($cart) {

                return response()->json(["result"=>"true"]);

            }

        }catch(CartServiceException $e){

            throw new ApiDoneErrorException($e->getMessage());
        }//end try

    }//end func create()

    /**
     * @api {post} /cart/remove_goods 删除购物车中某个商品.
     * @apiName Cart/removeGoods
     * @apiGroup Cart
     * @apiDescription 删除购物车中的商品.
     * @apiParam {String[]} goods_ids  要删除的商品id数组.
     *
     * @apiSuccess {String}  message      返回信息
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //成功删除购物车的商品
     * {
     *   "result": "true"
     * }
     *
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //删除购物车的商品失败
     * {
     *   "message": "当前用户类型不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //删除购物车的商品失败
     * {
     *   "message": "当前用户类型值不能为空"
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 202
     * //删除购物车的商品失败
     * {
     *   "message": "要删除的商品数组不能为空"
     * }
     */
    public function removeGoods(Request $request, CartService $cartService){

        $type = $this->getTypeFiled();
        $reference_id = $this->reference_id;

        if (empty($type)) {

            throw new ApiInputErrorException("当前用户类型不能为空");
        }//end if()

        if (empty($reference_id)) {

            throw new ApiInputErrorException("当前用户类型值不能为空");
        }//end if()

        try {

            $result = $cartService->removeGoods($request->goods_ids);

            return response()->json(["result"=>"true"]);

        } catch (CartServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());
        }


    }//end func removeGoods


    /**
     * @api {post} /cart/get_info 获取购物车详情.
     * @apiName Cart/getInfo
     * @apiGroup Cart
     * @apiDescription 获取购物车详情.
     *
     * @apiSuccess {Object}  Object         购物车的信息
     * @apiSuccess {number}  Object.total_number         购物车中所有商品总数量
     * @apiSuccess {number}  Object.total_number_not     购物车中下架、删除、无库存等商品的总数量
     * @apiSuccess {number}  Object.total_price          购物车中所有商品总价格
     * @apiSuccess {number}  Object.total_tariff_price         购物车中所有商品总关税              购物车中所有e贸易商品总关税
     * @apiSuccess {number}  Object.total_discount_price                所有商品的折扣总金额
     * @apiSuccess {number}  Object.pay_price                所有商品总价格（折扣后的）
     * @apiSuccess {Object[eshopGoods]}  Object.eshopGoods         购物车中所有e贸易商品信息
     * @apiSuccess {number}  Object.eshopGoods.total_discount_price                折扣金额总和
     * @apiSuccess {number}  Object.eshopGoods.total_number                购物车中所有e贸易商品总数量
     * @apiSuccess {number}  Object.eshopGoods.total_number_not                购物车中e贸易商品中所有下架、删除、无库存等商品的总数量
     * @apiSuccess {number}  Object.eshopGoods.total_price                购物车中所有e贸易商品总价格
     * @apiSuccess {number}  Object.eshopGoods.total_tariff_price                购物车中易商品总关税
     * @apiSuccess {Object[eshopGoods][shopGoods]}  Object.eshopGoods.shopGoods                购物车中所有e贸易商品数组中单个商品详细信息
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_id                商品id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.cat_id                  分类id
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_sn                商品编号
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_name              商品名称
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_pet_name          商品副标题
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_desc              商品简介
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.brand_id                品牌id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_number            商品数量
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_weight            商品重量
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.market_price            商品市场价格
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.shop_price              商品商城价格
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.keywords                商品关键词
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_content           商品内容
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_id                商品id
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_thumb             商品缩略图
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.resource                商品来源
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.type                    商品类型，0：一般商品，1：e贸易商品
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_gallery_id        商品相册id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.is_on_sale              商品是否上架，1：上架；2：下架
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.add_time                商品添加时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.sort_order              商品排序
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.is_delete               商品是否已被删除（目前不用）
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.is_home_recommend       商品是否首页推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.is_cat_recommend        商品是否分类推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.shop_id                 商品所属商铺id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.sales_num               商品销售数量
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.deleted_at                商品删除时间（默认为空，如果不为空则该商品已被删除）
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.search_keywords         商品搜索关键词
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.warehouse_id            商品所在仓库id（暂不可用）
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.created_at              商品创建时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.updated_at              商品编辑/更新时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.audit_status            商品核审状态：0：未审核；1：审核通过；2：审核不通过
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_bar_code          商品条形码
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.cart_id                 购物车id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.user_id                 用户id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.session_id              session id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.buy_num                 商品购买数量
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_attr              商品属性描述
     * @apiSuccess {String[]}  Object.eshopGoods.shopGoods.goods_attr_id           商品属性ID集合
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.cat_name                商品所属分类名称
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.cat_desc                商品所属分类描述
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.parent_id               商品所属分类父ID
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.measure_unit            计量单位
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_type              分类搜索属性
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.tariff_percent          关税比例
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.excise_duty_percent     消费税
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.value_added_tax_percent 增值税
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.is_recommend            否是隐藏：0：隐藏；1：显示
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.cat_picture              分类展示图片
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.tariff_price             商品关税（单个商品总关税）
     * @apiSuccess {Object[]}  Object.eshopGoods.shopGoods.discount             商品可以使用最大折扣信息
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.discount_id             折扣id
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.discount.discount_name             折扣名称
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.discount.discount_label             折扣标签
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.discount_type             折扣类型，1：全场通用；2：商品；3：分类；4：店铺；5：品牌
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.discount_value             折扣百分比，1-99
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.limit_amount             每位用户限用商品数量，0：不限；大于0表示限用个数
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.start_time             折扣开始时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.end_time               折扣结束时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.deleted_at             折扣删除时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.created_at             折扣创建时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.updated_at             折扣更新时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_real_price                商品折扣后的单价
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_discount_price            商品折扣后的总价
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.mobile_goods_thumb      商品图片完整地址
     * @apiSuccess {String}  Object.eshopGoods.pay_price      支付价格
     * @apiSuccess {Object[cshopGoods]}  Object.cshopGoods         购物车中所有一般商品信息
     * @apiSuccess {number}  Object.cshopGoods.total_discount_price                折扣金额总和
     * @apiSuccess {number}  Object.cshopGoods.total_number                购物车中所有一般商品总数量
     * @apiSuccess {number}  Object.cshopGoods.total_number_not                购物车中一般商品中所有下架、删除、无库存等商品的总数量
     * @apiSuccess {number}  Object.cshopGoods.total_price                购物车中所有一般商品总价格
     * @apiSuccess {number}  Object.cshopGoods.total_tariff_price                购物车中所有一般商品总关税
     * @apiSuccess {Object[cshopGoods][shopGoods]}  Object.cshopGoods.shopGoods                购物车中所有一般商品数组中单个商品详细信息
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_id                商品id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.cat_id                  分类id
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_sn                商品编号
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_name              商品名称
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_pet_name          商品副标题
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_desc              商品简介
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.brand_id                品牌id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_number            商品数量
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_weight            商品重量
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.market_price            商品市场价格
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.shop_price              商品商城价格
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.keywords                商品关键词
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_content           商品内容
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_id                商品id
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_thumb             商品缩略图
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.resource                商品来源
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.type                    商品类型，0：一般商品，1：e贸易商品
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_gallery_id        商品相册id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.is_on_sale              商品是否上架，1：上架；2：下架
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.add_time                商品添加时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.sort_order              商品排序
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.is_delete               商品是否已被删除（目前不用）
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.is_home_recommend       商品是否首页推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.is_cat_recommend        商品是否分类推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.shop_id                 商品所属商铺id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.sales_num               商品销售数量
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.deleted_at                商品删除时间（默认为空，如果不为空则该商品已被删除）
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.search_keywords         商品搜索关键词
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.warehouse_id            商品所在仓库id（暂不可用）
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.created_at              商品创建时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.updated_at              商品编辑/更新时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.audit_status            商品核审状态：0：未审核；1：审核通过；2：审核不通过
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_bar_code          商品条形码
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.cart_id                 购物车id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.user_id                 用户id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.session_id              session id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.buy_num                 商品购买数量
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_attr              商品属性描述
     * @apiSuccess {String[]}  Object.cshopGoods.shopGoods.goods_attr_id           商品属性ID集合
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.cat_name                商品所属分类名称
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.cat_desc                商品所属分类描述
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.parent_id               商品所属分类父ID
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.measure_unit            计量单位
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_type              分类搜索属性
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.tariff_percent          关税比例
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.is_recommend            否是隐藏：0：隐藏；1：显示
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.cat_picture              分类展示图片
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.tariff_price             商品关税（单个商品总关税）
     * @apiSuccess {Object[]}  Object.cshopGoods.shopGoods.discount             商品可以使用最大折扣信息
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.discount_id             折扣id
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.discount.discount_name             折扣名称
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.discount.discount_label             折扣标签
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.discount_type             折扣类型，1：全场通用；2：商品；3：分类；4：店铺；5：品牌
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.discount_value             折扣百分比，1-99
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.limit_amount             每位用户限用商品数量，0：不限；大于0表示限用个数
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.start_time             折扣开始时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.end_time               折扣结束时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.deleted_at             折扣删除时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.created_at             折扣创建时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.updated_at             折扣更新时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_real_price                商品折扣后的单价
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_discount_price            商品折扣后的总价
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.mobile_goods_thumb      商品图片完整地址
     * @apiSuccess {String}  Object.cshopGoods.pay_price      支付价格
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //成功获取购物车的商品
     * {
     *   "total_number": 4,
     *   "total_number_not": 0,
     *   "total_price": 264,
     *   "total_tariff_price": 0,
     *   "total_discount_price": 52.8,
     *   "pay_price": 211.2,
     *   "eshopGoods": {
     *     "total_discount_price": 13.2,
     *     "total_number": 2,
     *     "total_number_not": 0,
     *     "total_price": "66.00",
     *     "total_tariff_price": "33.00",
     *     "shopGoods": [
     *       {
     *         "goods_id": 1343,
     *         "cat_id": 398,
     *         "goods_sn": "33",
     *         "goods_name": "33",
     *         "goods_pet_name": "33",
     *         "goods_desc": "33",
     *         "brand_id": 74,
     *         "goods_number": 33,
     *         "goods_weight": "0.000",
     *         "market_price": "33.00",
     *         "shop_price": "33.00",
     *         "keywords": "香水,女士",
     *         "goods_content": "",
     *         "goods_thumb": "",
     *         "resource": "33",
     *         "type": 1,
     *         "goods_gallery_id": 0,
     *         "is_on_sale": 1,
     *         "add_time": 0,
     *         "sort_order": 100,
     *         "is_delete": 0,
     *         "is_home_recommend": 0,
     *         "is_cat_recommend": 0,
     *         "shop_id": 1,
     *         "sales_num": 0,
     *         "deleted_at": null,
     *         "search_keywords": "33,JJBIO,美妆个护,美容彩妆,香水/香氛,香水,香氛,女士香水,香水,女士",
     *         "warehouse_id": 0,
     *         "created_at": "2016-02-19 10:10:49",
     *         "updated_at": "2016-03-16 12:03:42",
     *         "audit_status": 1,
     *         "goods_bar_code": null,
     *         "cart_id": 114,
     *         "user_id": 0,
     *         "session_id": "d0dca34cce180ad3f86f1230f4c28a343a3cadcf",
     *         "buy_num": 2,
     *         "goods_attr": "",
     *         "goods_attr_id": "",
     *         "cat_name": "女士香水",
     *         "cat_desc": "3级分类女士香水",
     *         "parent_id": 397,
     *         "measure_unit": "盒",
     *         "goods_type": 0,
     *         "tariff_percent": 50,
     *         "excise_duty_percent": 0,
     *         "value_added_tax_percent": 17,
     *         "is_recommend": 1,
     *         "cat_picture": null,
     *         "tariff_price": "17.85",
     *         "discount": {
     *           "discount_id": 9,
     *           "discount_name": "去掉限时折扣中的数量限制哈",
     *           "discount_label": "去掉限时折扣中的数量限制",
     *           "discount_type": 1,
     *           "discount_value": 80,
     *           "limit_amount": 0,
     *           "start_time": "2016-07-23 15:55:40",
     *           "end_time": "2016-07-31 00:00:00",
     *           "deleted_at": null,
     *           "created_at": "2016-07-22 16:09:36",
     *           "updated_at": "2016-07-23 15:55:40"
     *         },
     *         "goods_real_price": "26.40",
     *         "goods_discount_price": "52.80",
     *         "mobile_goods_thumb": ""
     *       }
     *     ],
     *     "pay_price": 52.8
     *   },
     *   "cshopGoods": {
     *     "total_discount_price": 39.6,
     *     "total_number": 2,
     *     "total_number_not": 0,
     *     "total_price": "198.00",
     *     "total_tariff_price": "0.00",
     *     "shopGoods": [
     *       {
     *         "goods_id": 1346,
     *         "cat_id": 400,
     *         "goods_sn": "010101",
     *         "goods_name": "莲花香",
     *         "goods_pet_name": "莲花香牌男士香水",
     *         "goods_desc": "莲花香牌男士香水",
     *         "brand_id": 79,
     *         "goods_number": 100,
     *         "goods_weight": "0.000",
     *         "market_price": "199.00",
     *         "shop_price": "99.00",
     *         "keywords": "香水,中性",
     *         "goods_content": "",
     *         "goods_thumb": "",
     *         "resource": "中国",
     *         "type": 0,
     *         "goods_gallery_id": 0,
     *         "is_on_sale": 1,
     *         "add_time": 0,
     *         "sort_order": 100,
     *         "is_delete": 0,
     *         "is_home_recommend": 0,
     *         "is_cat_recommend": 0,
     *         "shop_id": 5,
     *         "sales_num": 0,
     *         "deleted_at": null,
     *         "search_keywords": ",4ASCENT,美妆个护,美容彩妆,香水/香氛,香水,香氛,中性香水,香水,中性",
     *         "warehouse_id": 0,
     *         "created_at": "2016-04-01 14:13:29",
     *         "updated_at": "2016-04-01 14:49:08",
     *         "audit_status": 1,
     *         "goods_bar_code": "010101",
     *         "cart_id": 115,
     *         "user_id": 0,
     *         "session_id": "d0dca34cce180ad3f86f1230f4c28a343a3cadcf",
     *         "buy_num": 2,
     *         "goods_attr": "",
     *         "goods_attr_id": "",
     *         "cat_name": "中性香水",
     *         "cat_desc": "3级分类中性香水",
     *         "parent_id": 397,
     *         "measure_unit": "盒",
     *         "goods_type": 0,
     *         "tariff_percent": 50,
     *         "is_recommend": 1,
     *         "cat_picture": null,
     *         "tariff_price": "",
     *         "discount": {
     *           "discount_id": 9,
     *           "discount_name": "去掉限时折扣中的数量限制哈",
     *           "discount_label": "去掉限时折扣中的数量限制",
     *           "discount_type": 1,
     *           "discount_value": 80,
     *           "limit_amount": 0,
     *           "start_time": "2016-07-23 15:55:40",
     *           "end_time": "2016-07-31 00:00:00",
     *           "deleted_at": null,
     *           "created_at": "2016-07-22 16:09:36",
     *           "updated_at": "2016-07-23 15:55:40"
     *         },
     *         "goods_real_price": "79.20",
     *         "goods_discount_price": "158.40",
     *         "mobile_goods_thumb": "http://afutest.img-cn-qingdao.aliyuncs.com/8a75e0739bad792a326d8153dc6306fe.jpg"
     *       }
     *     ],
     *     "pay_price": 158.4
     *   }
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 406
     * //获取购物车的商品失败
     * {
     *   "status": 406,
     *   "message": "当前用户类型不能为空",
     *   "data": ""
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 406
     * //获取购物车的商品失败
     * {
     *   "status": 406,
     *   "message": "当前用户类型值不能为空",
     *   "data": ""
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 500
     * //获取购物车的商品失败
     * {
     *   "status": 500,
     *   "message": "type参数类型错误,必须为SESSION或USER",
     *   "data": ""
     * }
     */
    public function getInfo(Request $request, CartService $cartService){

        $type = $this->getTypeFiled();
        $reference_id = $this->reference_id;

        if (empty($type)) {

            throw new ApiInputErrorException("当前用户类型不能为空");
        }//end if()

        if (empty($reference_id)) {

            throw new ApiInputErrorException("当前用户类型值不能为空");
        }//end if()

        try {

            $return = $cartService->getInfo();

            return response()->json($return);

        } catch (CartServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());
        }


    }//end func getInfo()

    /**
     * @api {post} /cart/link_session_to_user 游客用户转化为登陆用户.
     * @apiName Cart/linkSessionToUser
     * @apiGroup Cart
     * @apiDescription 游客用户转化为登陆用户.
     *
     * @apiParam {Number} session_id  session id.
     * @apiParam {Number} user_id     user_id.
     *
     * @apiSuccess {String}  message      返回信息
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //游客用户转化为登陆用户成功
     * {
     *   "result": "true"
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 406
     * //游客用户转化为登陆用户失败
     * {
     *     "message": "session_id不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 406
     * //游客用户转化为登陆用户失败
     * {
     *     "message": "user_id不能为空"
     * }
     */
    public function linkSessionToUser(Request $request, CartService $cartService){

        $session_id = $request->get('session_id');

        if (empty($session_id)) {

            throw new ApiInputErrorException("session_id不能为空");
        }//end if()


        if (isset($request->apiUser->get()->id)) {

            $user_id = $request->apiUser->get()->id;
        }else{

            throw new ApiInputErrorException("user_id不能为空");
        }

        //$reference_id = $request->apiSession->session_id;

        try {

            $result = $cartService->linkSessionToUser($session_id,$user_id);

            return response()->json(["result"=>"true"]);

        } catch (CartServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());
        }

    }//end func linkSessionToUser

    /**
     * @api {post} /cart/get_goods_info 获取商品数组中商品总数、总价、总关税等信息.
     * @apiName Cart/getGoodsInfo
     * @apiGroup Cart
     * @apiDescription 获取商品数组中商品总数、总价、总关税等信息.
     *
     * @apiParam {String[]} cartGoods  商品数组array(0=>array('goods_id'=>1, 'buy_num'=>2), 1=>array('goods_id'=>2, 'buy_num'=>2)).
     *
     * @apiSuccess {Object[]}  return      商品数组中商品总数、总价、总关税等信息
     * @apiSuccess {number}  Object.total_number         所有商品总数量
     * @apiSuccess {number}  Object.total_number_not     下架、删除、无库存等商品的总数量
     * @apiSuccess {number}  Object.total_price          所有商品总价格
     * @apiSuccess {number}  Object.total_tariff_price         所有商品总关税
     * @apiSuccess {number}  Object.total_discount_price       所有商品的折扣总金额
     * @apiSuccess {number}  Object.pay_price                  所有商品总价格（折扣后的）
     * @apiSuccess {Object[eshopGoods]}  Object.eshopGoods         所有e贸易商品信息
     * @apiSuccess {number}  Object.eshopGoods.total_discount_price                所有商品的折扣总金额
     * @apiSuccess {number}  Object.eshopGoods.total_number                所有e贸易商品总数量
     * @apiSuccess {number}  Object.eshopGoods.total_number_not                e贸易商品中所有下架、删除、无库存等商品的总数量
     * @apiSuccess {number}  Object.eshopGoods.total_price                所有e贸易商品总价格
     * @apiSuccess {number}  Object.eshopGoods.total_tariff_price                所有e贸易商品总关税
     * @apiSuccess {Object[eshopGoods][shopGoods]}  Object.eshopGoods.shopGoods                所有e贸易商品数组中单个商品详细信息
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_id                商品id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.cat_id                  分类id
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_sn                商品编号
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_name              商品名称
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_pet_name          商品副标题
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_desc              商品简介
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.brand_id                品牌id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_number            商品数量
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_weight            商品重量
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.market_price            商品市场价格
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.shop_price              商品商城价格
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.keywords                商品关键词
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_content           商品内容
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_id                商品id
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_thumb             商品缩略图
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.resource                商品来源
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.type                    商品类型，0：一般商品，1：e贸易商品
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_gallery_id        商品相册id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.is_on_sale              商品是否上架，1：上架；2：下架
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.add_time                商品添加时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.sort_order              商品排序
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.is_delete               商品是否已被删除（目前不用）
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.is_home_recommend       商品是否首页推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.is_cat_recommend        商品是否分类推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.shop_id                 商品所属商铺id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.sales_num               商品销售数量
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.deleted_at                商品删除时间（默认为空，如果不为空则该商品已被删除）
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.search_keywords         商品搜索关键词
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.warehouse_id            商品所在仓库id（暂不可用）
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.created_at              商品创建时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.updated_at              商品编辑/更新时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.audit_status            商品核审状态：0：未审核；1：审核通过；2：审核不通过
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_bar_code          商品条形码
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.cart_id                 购物车id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.user_id                 用户id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.session_id              session id
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.buy_num                 商品购买数量
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.goods_attr              商品属性描述
     * @apiSuccess {String[]}  Object.eshopGoods.shopGoods.goods_attr_id           商品属性ID集合
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.cat_name                商品所属分类名称
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.cat_desc                商品所属分类描述
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.parent_id               商品所属分类父ID
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.measure_unit            计量单位
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_type              分类搜索属性
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.tariff_percent          关税比例
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.is_recommend            否是隐藏：0：隐藏；1：显示
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.cat_picture             分类展示图片
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.tariff_price             商品关税（单个商品总关税）
     * @apiSuccess {Object[]}  Object.eshopGoods.shopGoods.discount              商品可以使用最大折扣信息
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.discount_id            折扣id
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.discount.discount_name          折扣名称
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.discount.discount_label         折扣标签
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.discount_type          折扣类型，1：全场通用；2：商品；3：分类；4：店铺；5：品牌
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.discount_value         折扣百分比，1-99
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.limit_amount           每位用户限用商品数量，0：不限；大于0表示限用个数
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.start_time             折扣开始时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.end_time               折扣结束时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.deleted_at             折扣删除时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.created_at             折扣创建时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.discount.updated_at             折扣更新时间
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_real_price                商品折扣后的单价
     * @apiSuccess {Number}  Object.eshopGoods.shopGoods.goods_discount_price            商品折扣后的总价
     * @apiSuccess {String}  Object.eshopGoods.shopGoods.mobile_goods_thumb      商品图片完整地址
     * @apiSuccess {String}  Object.eshopGoods.pay_price      支付价格
     * @apiSuccess {Object[cshopGoods]}  Object.cshopGoods         所有一般商品信息
     * @apiSuccess {number}  Object.cshopGoods.total_number                所有一般商品总数量
     * @apiSuccess {number}  Object.cshopGoods.total_number_not                一般商品中所有下架、删除、无库存等商品的总数量
     * @apiSuccess {number}  Object.cshopGoods.total_price                所有一般商品总价格
     * @apiSuccess {number}  Object.cshopGoods.total_tariff_price                所有一般商品总关税
     * @apiSuccess {Object[cshopGoods][shopGoods]}  Object.cshopGoods.shopGoods                所有一般商品数组中单个商品详细信息
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_id                商品id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.cat_id                  分类id
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_sn                商品编号
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_name              商品名称
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_pet_name          商品副标题
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_desc              商品简介
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.brand_id                品牌id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_number            商品数量
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_weight            商品重量
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.market_price            商品市场价格
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.shop_price              商品商城价格
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.keywords                商品关键词
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_content           商品内容
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_thumb             商品缩略图
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.resource                商品来源
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.type                    商品类型，0：一般商品，1：e贸易商品
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_gallery_id        商品相册id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.is_on_sale              商品是否上架，1：上架；2：下架
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.add_time                商品添加时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.sort_order              商品排序
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.is_delete               商品是否已被删除（目前不用）
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.is_home_recommend       商品是否首页推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.is_cat_recommend        商品是否分类推荐，0：不推荐；1：推荐；
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.shop_id                 商品所属商铺id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.sales_num               商品销售数量
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.deleted_at                商品删除时间（默认为空，如果不为空则该商品已被删除）
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.search_keywords         商品搜索关键词
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.warehouse_id            商品所在仓库id（暂不可用）
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.created_at              商品创建时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.updated_at              商品编辑/更新时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.audit_status            商品核审状态：0：未审核；1：审核通过；2：审核不通过
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_bar_code          商品条形码
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.cart_id                 购物车id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.user_id                 用户id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.session_id              session id
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.buy_num                 商品购买数量
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.goods_attr              商品属性描述
     * @apiSuccess {String[]}  Object.cshopGoods.shopGoods.goods_attr_id           商品属性ID集合
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.cat_name                商品所属分类名称
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.cat_desc                商品所属分类描述
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.parent_id               商品所属分类父ID
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.measure_unit            计量单位
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_type              分类搜索属性
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.tariff_percent          关税比例
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.is_recommend            否是隐藏：0：隐藏；1：显示
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.cat_picture             分类展示图片
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.tariff_price             商品关税（单个商品总关税）
     * @apiSuccess {Object[]}  Object.cshopGoods.shopGoods.discount              商品可以使用最大折扣信息
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.discount_id            折扣id
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.discount.discount_name          折扣名称
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.discount.discount_label         折扣标签
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.discount_type          折扣类型，1：全场通用；2：商品；3：分类；4：店铺；5：品牌
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.discount_value         折扣百分比，1-99
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.limit_amount           每位用户限用商品数量，0：不限；大于0表示限用个数
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.start_time             折扣开始时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.end_time               折扣结束时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.deleted_at             折扣删除时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.created_at             折扣创建时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.discount.updated_at             折扣更新时间
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_real_price                商品折扣后的单价
     * @apiSuccess {Number}  Object.cshopGoods.shopGoods.goods_discount_price            商品折扣后的总价
     * @apiSuccess {String}  Object.cshopGoods.shopGoods.mobile_goods_thumb      商品图片完整地址
     * @apiSuccess {String}  Object.cshopGoods.pay_price      支付价格
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //获取商品数组中商品总数、总价、总关税等信息成功
     * {
     *     "total_number": 3,
     *     "total_number_not": 0,
     *     "total_price": 165,
     *     "total_tariff_price": 0,
     *     "total_discount_price": 33,
     *     "pay_price": 132,
     *     "eshopGoods": {
     *     "total_discount_price": 13.2,
     *       "total_number": 2,
     *       "total_number_not": 0,
     *       "total_price": "66.00",
     *       "total_tariff_price": "33.00",
     *       "shopGoods": [
     *         {
     *           "goods_id": 1343,
     *           "cat_id": 398,
     *           "goods_sn": "33",
     *           "goods_name": "33",
     *           "goods_pet_name": "33",
     *           "goods_desc": "33",
     *           "brand_id": 74,
     *           "goods_number": 33,
     *           "goods_weight": "0.000",
     *           "market_price": "33.00",
     *           "shop_price": "33.00",
     *           "keywords": "香水,女士",
     *           "goods_content": "",
     *           "goods_thumb": "8a75e0739bad792a326d8153dc6306fe",
     *           "resource": "33",
     *           "type": 1,
     *           "goods_gallery_id": 0,
     *           "is_on_sale": 1,
     *           "add_time": 0,
     *           "sort_order": 100,
     *           "is_delete": 0,
     *           "is_home_recommend": 0,
     *           "is_cat_recommend": 0,
     *           "shop_id": 1,
     *           "sales_num": 0,
     *           "deleted_at": null,
     *           "search_keywords": "33,JJBIO,美妆个护,美容彩妆,香水/香氛,香水,香氛,女士香水,香水,女士",
     *           "warehouse_id": 0,
     *           "created_at": "2016-02-19 10:10:49",
     *           "updated_at": "2016-03-16 12:03:42",
     *           "audit_status": 1,
     *           "goods_bar_code": null,
     *           "cart_id": 118,
     *           "user_id": 59,
     *           "session_id": "",
     *           "buy_num": 5,
     *           "goods_attr": "",
     *           "goods_attr_id": "",
     *           "cat_name": "女士香水",
     *           "cat_desc": "3级分类女士香水",
     *           "parent_id": 397,
     *           "measure_unit": "盒",
     *           "goods_type": 0,
     *           "tariff_percent": 50,
     *           "is_recommend": 1,
     *           "cat_picture": null,
     *           "tariff_price": "17.85",
     *           "discount": {
     *             "discount_id": 9,
     *             "discount_name": "去掉限时折扣中的数量限制哈",
     *             "discount_label": "去掉限时折扣中的数量限制",
     *             "discount_type": 1,
     *             "discount_value": 80,
     *             "limit_amount": 0,
     *             "start_time": "2016-07-23 15:55:40",
     *             "end_time": "2016-07-31 00:00:00",
     *             "deleted_at": null,
     *             "created_at": "2016-07-22 16:09:36",
     *             "updated_at": "2016-07-23 15:55:40"
     *           },
     *           "goods_real_price": "44.00",
     *           "goods_discount_price": "88.00",
     *           "mobile_goods_thumb": "http://afutest.img-cn-qingdao.aliyuncs.com/8a75e0739bad792a326d8153dc6306fe.jpg"
     *         }
     *       ],
     *       "pay_price": 88.00
     *     },
     *     "cshopGoods": {
     *       "total_number": 1,
     *       "total_number_not": 0,
     *       "total_price": "99.00",
     *       "total_tariff_price": "0.00",
     *       "shopGoods": [
     *         {
     *           "goods_id": 1346,
     *           "cat_id": 400,
     *           "goods_sn": "010101",
     *           "goods_name": "莲花香",
     *           "goods_pet_name": "莲花香牌男士香水",
     *           "goods_desc": "莲花香牌男士香水",
     *           "brand_id": 79,
     *           "goods_number": 100,
     *           "goods_weight": "0.000",
     *           "market_price": "199.00",
     *           "shop_price": "99.00",
     *           "keywords": "香水,中性",
     *           "goods_content": "",
     *           "goods_thumb": "",
     *           "resource": "中国",
     *           "type": 0,
     *           "goods_gallery_id": 0,
     *           "is_on_sale": 1,
     *           "add_time": 0,
     *           "sort_order": 100,
     *           "is_delete": 0,
     *           "is_home_recommend": 0,
     *           "is_cat_recommend": 0,
     *           "shop_id": 5,
     *           "sales_num": 0,
     *           "deleted_at": null,
     *           "search_keywords": ",4ASCENT,美妆个护,美容彩妆,香水/香氛,香水,香氛,中性香水,香水,中性",
     *           "warehouse_id": 0,
     *           "created_at": "2016-04-01 14:13:29",
     *           "updated_at": "2016-04-01 14:49:08",
     *           "audit_status": 1,
     *           "goods_bar_code": "010101",
     *           "cart_id": 119,
     *           "user_id": 59,
     *           "session_id": "",
     *           "buy_num": 6,
     *           "goods_attr": "",
     *           "goods_attr_id": "",
     *           "cat_name": "中性香水",
     *           "cat_desc": "3级分类中性香水",
     *           "parent_id": 397,
     *           "measure_unit": "盒",
     *           "goods_type": 0,
     *           "tariff_percent": 50,
     *           "is_recommend": 1,
     *           "cat_picture": null,
     *           "tariff_price": "",
     *           "discount": {
     *             "discount_id": 9,
     *             "discount_name": "去掉限时折扣中的数量限制哈",
     *             "discount_label": "去掉限时折扣中的数量限制",
     *             "discount_type": 1,
     *             "discount_value": 80,
     *             "limit_amount": 0,
     *             "start_time": "2016-07-23 15:55:40",
     *             "end_time": "2016-07-31 00:00:00",
     *             "deleted_at": null,
     *             "created_at": "2016-07-22 16:09:36",
     *             "updated_at": "2016-07-23 15:55:40"
     *           },
     *           "goods_real_price": "142.40",
     *           "goods_discount_price": "142.40",
     *           "mobile_goods_thumb": ""
     *         }
     *       ],
     *       "pay_price": 230.4
     *     }
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //获取商品数组中商品总数、总价、总关税等信息失败
     * {
     *   "status": 400,
     *   "message": "要计算的商品个数不能为空",
     *   "data": ""
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //获取商品数组中商品总数、总价、总关税等信息失败
     * {
     *   "status": 400,
     *   "message": "当前用户类型不能为空",
     *   "data": ""
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 400
     * //获取商品数组中商品总数、总价、总关税等信息失败
     * {
     *   "status": 400,
     *   "message": "当前用户类型值不能为空",
     *   "data": ""
     * }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 500
     * //获取商品数组中商品总数、总价、总关税等信息失败
     * {
     *   "status": 500,
     *   "message": "type参数类型错误,必须为SESSION或USER",
     *   "data": ""
     * }
     */
    public function getGoodsInfo(Request $request, CartService $cartService){

        $cartGoods = $request->cartGoods;
        if (empty($cartGoods)) {

            throw new ApiInputErrorException("要计算的商品个数不能为空");
        }//end if()

        $type = $this->getTypeFiled();
        $reference_id = $this->reference_id;

        if (empty($type)) {

            throw new ApiInputErrorException("当前用户类型不能为空");
        }//end if()

        if (empty($reference_id)) {

            throw new ApiInputErrorException("当前用户类型值不能为空");
        }//end if()


        try {

            $return= $cartService->getGoodsInfo($cartGoods);
            return response()->json($return);

        } catch (Exception $e) {

            throw new ApiDoneErrorException($e->getMessage());

        }




    }//end func getGoodsInfo

    /**
     * @api {post} /cart/update_number 更新购物车商品中的商品数量.
     * @apiName Cart/updateNumber
     * @apiGroup Cart
     * @apiDescription 更新购物车商品中的商品数量.
     *
     * @apiParam {String} goods_id  商品id.
     * @apiParam {Number} buy_num   商品数量.
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //更新商品数量成功
     * {
     *   "result": "true"
     * }
     *
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //更新商品数量失败
     * {
     *   "status": 400,
     *   "message": "商品id不能为空",
     *   "data": ""
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //更新商品数量失败
     * {
     *   "status": 400,
     *   "message": "商品数量不能为空或为0",
     *   "data": ""
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 402
     * //更新商品数量失败
     * {
     *   "status": 402,
     *   "message": "该商品库存不足",
     *   "data": ""
     * }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 402
     * //更新商品数量失败
     * {
     *   "status": 402,
     *   "message": "该商品已下架",
     *   "data": ""
     * }
     */
    public function updateNumber(Request $request, CartService $cartService){

        $goods_id = $request->goods_id;

        $buy_num = $request->buy_num;

        if (empty($goods_id)) {

            throw new ApiInputErrorException("商品id不能为空");

        }//end if()

        if (empty($buy_num)) {

            throw new ApiInputErrorException("商品数量不能为空或为0");

        }//end if()

        try {

            $return= $cartService->updateNumber($goods_id,$buy_num);

            if ($return) {

                return response()->json(["result"=>"true"]);

            }


        } catch (Exception $e) {

            throw new ApiDoneErrorException($e->getMessage());

        }

    }//end func updateNumber

    /**
     * @api {post} /cart/update_batch 批量更新购物车.
     * @apiName Cart/updateBatch
     * @apiGroup Cart
     * @apiDescription 批量更新购物车.
     *
     * @apiParam {Object[]} batchGoods  商品数组，如：array(0=>array('goods_id'=>1, 'buy_num'=>2), 1=>array('goods_id'=>2, 'buy_num'=>2)).
     * @apiParam {Number} batchGoods.goods_id  商品id.
     * @apiParam {Number} batchGoods.buy_num   商品数量.
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //更新商品数量成功
     * {
     *   "result": "true"
     * }
     *
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //更新商品数量失败
     * {
     *   "status": 400,
     *   "message": "要更新的商品不能为空",
     *   "data": ""
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //更新商品数量失败
     * {
     *   "status": 400,
     *   "message": "商品id不能为空",
     *   "data": ""
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 400
     * //更新商品数量失败
     * {
     *   "status": 400,
     *   "message": "商品数量不能为空或为0",
     *   "data": ""
     * }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 402
     * //更新商品数量失败
     * {
     *   "status": 402,
     *   "message": "该商品库存不足",
     *   "data": ""
     * }
     * @apiErrorExample Error-Response 05:
     * HTTP/1.1 402
     * //更新商品数量失败
     * {
     *   "status": 402,
     *   "message": "该商品已下架",
     *   "data": ""
     * }
     */
    public function updateBatch(Request $request, CartService $cartService){

        $batchGoods = $request->batchGoods;

        if (empty($batchGoods)) {

            throw new ApiInputErrorException("要更新的商品不能为空");
        }

        DB::beginTransaction();//开启事务,开始批量操作
        try {

            foreach ($batchGoods as $key => $value) {

                $goods_id = $value['goods_id'];

                $buy_num = $value['buy_num'];

                if (empty($goods_id)) {

                    throw new ApiInputErrorException("商品id不能为空");
                }

                if (empty($buy_num)) {

                    throw new ApiInputErrorException("商品数量不能为空或为0");
                }

                try {

                    $return= $cartService->updateNumber($goods_id,$buy_num);

                } catch (Exception $e) {

                    throw new ApiDoneErrorException($e->getMessage());
                }
            }

        }catch (Exception $ex){
            DB::rollback();
            throw new ApiDoneErrorException('更新购物车出错！');
        }

        DB::commit();

        return response()->json(["result"=>"true"]);

    }//end func updateBatch


}